package com.daxt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArchivesTestingStandardMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesTestingStandard;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.service.TestingService;



@Service
public class TestingServiceImpl implements TestingService{

	@Autowired
	private ArchivesTestingStandardMapper archivesTestingStandardMapper;
	@Autowired
	private ArchivesDocMapper archivesDocMapper;
	@Autowired
	private ArchivesTypeMapper archivesTypeMapper;
	@Autowired
	private RedisUtil redisUtil;
	
	@Async("cloudExecutor")
	@Override
	public void StandardTesting(String typeId,List<ArchivesTestingStandard> list,String taskId)  {
		Map<Object,Object> result=new HashMap<>();
		result.put("state", "0");
		ArchivesType typeEntity = archivesTypeMapper.selectById(typeId);
		for(ArchivesTestingStandard archivesTestingStandard:list) {
			if("1".equals(archivesTestingStandard.getCtype())) {
				try {
				List<Long> ids = archivesTestingStandardMapper.selectFile(typeEntity.getFondsNum(),typeEntity.getTypeNum());
				redisUtil.set(taskId+""+archivesTestingStandard.getId(), ids,60*60);
				result.put(archivesTestingStandard.getId(),ids.size());
				redisUtil.hset("testing", taskId,result,60*60);
			}catch (Exception e) {
				e.getStackTrace();
				result.put(archivesTestingStandard.getId(),"-1");
				redisUtil.hset("testing", taskId,result,60*60);
			}
			}else if("2".equals(archivesTestingStandard.getCtype())) {
				try {
				QueryWrapper<ArchivesDoc> queryWrapper=new QueryWrapper<ArchivesDoc>();
				queryWrapper.eq("fondsNum", typeEntity.getFondsNum());
				queryWrapper.eq("typeNum",typeEntity.getTypeNum());
				queryWrapper.eq("fileType",archivesTestingStandard.getFileType());
				queryWrapper.eq("isdelete", "0");
				queryWrapper.last(" having count("+archivesTestingStandard.getDataColumn()+")> 1");
				queryWrapper.groupBy(archivesTestingStandard.getDataColumn());
				queryWrapper.select("id",archivesTestingStandard.getDataColumn());
				List<ArchivesDoc> ckValue = archivesDocMapper.selectList(queryWrapper);
				List<Long> ids=new ArrayList<Long>();
				List<Object> value=new ArrayList<Object>();
				if(ckValue!=null&&ckValue.size()>0) {
					for(ArchivesDoc doc:ckValue) {
						Map parseObject = JSON.parseObject(JSON.toJSONString(doc), Map.class);
						Object object = parseObject.get(archivesTestingStandard.getDataColumn());
						if(object!=null)
							value.add(object);
					}
					QueryWrapper<ArchivesDoc> queryWrapperResult=new QueryWrapper<ArchivesDoc>();
					queryWrapperResult.eq("fondsNum", typeEntity.getFondsNum());
					queryWrapperResult.eq("typeNum",typeEntity.getTypeNum());
					queryWrapperResult.eq("fileType",archivesTestingStandard.getFileType());
					queryWrapperResult.eq("isdelete", "0");
					queryWrapperResult.eq("state","0");
					queryWrapperResult.in(archivesTestingStandard.getDataColumn(),value );
					queryWrapperResult.select("id","state");
					List<ArchivesDoc> docList = archivesDocMapper.selectList(queryWrapperResult);
					if(docList!=null&&docList.size()>0) {
						for(ArchivesDoc temp:docList) {
							if("0".equals(temp.getState()))
							ids.add(temp.getId());
						}
					}
				}
				redisUtil.set(taskId+""+archivesTestingStandard.getId(), ids,60*60);
				result.put(archivesTestingStandard.getId(),ids.size());
				redisUtil.hset("testing", taskId,result,60*60);
				}catch (Exception e) {
					e.getStackTrace();
					result.put(archivesTestingStandard.getId(),"-1");
					redisUtil.hset("testing", taskId,result,60*60);
				}
			}else if("3".equals(archivesTestingStandard.getCtype())) {
				try {
				QueryWrapper<ArchivesDoc> queryWrapper=new QueryWrapper<>();
				queryWrapper.eq("fondsNum", typeEntity.getFondsNum());
				queryWrapper.eq("typeNum", typeEntity.getTypeNum());
				queryWrapper.last(" and "+archivesTestingStandard.getDataColumn()+" is null");
				queryWrapper.eq("fileType",archivesTestingStandard.getFileType());
				queryWrapper.select("id","state");
				List<ArchivesDoc> nullList = archivesDocMapper.selectList(queryWrapper);
				List<Long> ids=new ArrayList<Long>();
				if(nullList!=null&&nullList.size()>0) {
					for (ArchivesDoc standard : nullList) {
						if("0".equals(standard.getState()))
						ids.add(standard.getId());
					}
				}
				redisUtil.set(taskId+""+archivesTestingStandard.getId(),ids,60*60);
				result.put(archivesTestingStandard.getId(),ids.size());
				redisUtil.hset("testing", taskId,result,60*60);
				}catch (Exception e) {
					e.getStackTrace();
					result.put(archivesTestingStandard.getId(),"-1");
					redisUtil.hset("testing", taskId,result,60*60);
				}
			}else if("4".equals(archivesTestingStandard.getCtype())) {
				try {
				String cronValue = archivesTestingStandard.getCronValue();
				String cronc="";
		    	if(cronValue.contains("aconcata")) {
		    		String[] split = cronValue.split("aconcata");
		    		for (String string : split) {
						if(StringUtils.isEmpty(cronc)) {
							cronc=string;
						}else {
							cronc+=" and "+string;
						}
					}
		    	}else {
		    		cronc=cronValue;
		    	}
				QueryWrapper<ArchivesDoc> queryWrapper=new QueryWrapper<ArchivesDoc>();
				queryWrapper.eq("fondsNum", typeEntity.getFondsNum());
				queryWrapper.eq("typeNum", typeEntity.getTypeNum());
				queryWrapper.eq("fileType",archivesTestingStandard.getFileType());
				queryWrapper.last(" and "+cronc);
				queryWrapper.select("id","state");
				List<ArchivesDoc> condition = archivesDocMapper.selectList(queryWrapper);
				List<Long> ids=new ArrayList<Long>();
				if(condition!=null&&condition.size()>0) {
					for (ArchivesDoc conditionEntity : condition) {
						if("0".equals(conditionEntity.getState()))
						ids.add(conditionEntity.getId());
					}
				}
				redisUtil.set(taskId+""+archivesTestingStandard.getId(),ids,60*60);
				result.put(archivesTestingStandard.getId(),ids.size());
				redisUtil.hset("testing", taskId,result,60*60);
				}catch (Exception e) {
					e.getStackTrace();
					result.put(archivesTestingStandard.getId(),"-1");
					redisUtil.hset("testing", taskId,result,60*60);
				}
			}
		}
		result.put("state", "1");
		redisUtil.hset("testing", taskId,result,60*60);
	}
	
}
